#pragma once
#include <stdint.h>
#include <string.h>

#include <list>
#include <map>
#include <mutex>
#include <set>
#include <string>

#include "bondlib.h"
// 连续合约切换信息，国债期货

S_BONDLIB_EXPORT typedef struct _tag_CCFLOWINFOUNIT {
  uint32_t TFCDate;  // 连续合约切换日期
  char TFID[33];     // 连续合约对应的实际合约代码
  _tag_CCFLOWINFOUNIT() { memset(this, 0, sizeof(_tag_CCFLOWINFOUNIT)); }
  const bool operator<(const _tag_CCFLOWINFOUNIT& one) const {
    return (TFCDate < one.TFCDate);
  };
} CCFLOWINFOUNIT;

class S_BONDLIB_EXPORT CCCFlowInfo {
 public:
  CCCFlowInfo();
  CCCFlowInfo(const char* sTFCKey, const char* sTFCID, const char* sTFCName);
  ~CCCFlowInfo();

  void Clear();
  void SetTFCKey(const char* sTFCKey);
  void SetTFCID(const char* sTFCID);
  void SetTFCName(const char* sTFCName);

  const char* GetTFCKey();
  const char* GetTFCID();
  const char* GetTFCName();

  void Add(const char* sTFID, const char* TFCDate);
  void Sort();

  const char* GetCurrentTFID() const;
  uint32_t GetCurrentDate();

  const std::list<CCFLOWINFOUNIT>& GetFlowInfoList();
  class SizeGetter {
   public:
    static int GetItemRealSize(const CCCFlowInfo& info);
    static int GetItemAllSize(const CCCFlowInfo& info);
  };

 private:
  char m_TFCKey[33];   // 连续合约Key
  char m_TFCID[33];    // 连续合约代码
  char m_TFCName[65];  // 连续合约的名称
  std::list<CCFLOWINFOUNIT> m_list;
};

class S_BONDLIB_EXPORT CCCFlowManager {
 public:
  void Clear();
  int AddFlowInfo(const char* sTFCKey, const char* sTFCID, const char* sTFCName,
                  const char* sTFID, const char* TFCDate);
  int GetSize();

  const std::set<std::string>& GetTFCIDList();
  const CCCFlowInfo* GetCCFlowInfo(const char* sTFCID);
  std::mutex& GetMutex();

 public:
  void DumpStatus(int mode = 0) const;
  static CCCFlowManager& instance();

 private:
  CCCFlowManager();
  ~CCCFlowManager();

 private:
  std::set<std::string> m_TFCIDList;
  std::map<std::string, CCCFlowInfo> m_map;
  std::mutex m_mutex;
};
